//
//  LGLungFollowPatientCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2024/7/10.
//  选择患者

#import "LGLungFollowPatientCell.h"

@interface LGLungFollowPatientCell()

/**   */
@property (nonatomic , strong)UIButton *iconBtn;
/**   */
@property (nonatomic , strong)UIImageView *headPic;
/**   */
@property (nonatomic , strong)UILabel *nameLabel;
/**   */
@property (nonatomic , strong)UILabel *ageLabel;
/**   */
@property (nonatomic , strong)UILabel *timeLabel;
/**   */
@property (nonatomic , strong)NSMutableArray *itemArray;

@end

@implementation LGLungFollowPatientCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return  self;
}

-(void)setIsSelected:(BOOL)isSelected{
    _isSelected = isSelected;
    self.iconBtn.selected = isSelected;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    NSString *gender = LGNSString(dataDic[@"gender"]);
    UIImage *placeHolder = [gender isEqualToString:@"女"]?defaultPatientWomen:defaultPatientMan;
    [self.headPic sd_setImageWithURL:[NSURL URLWithString:LGNSString(dataDic[@"avatar_url"])] placeholderImage:placeHolder];
    self.nameLabel.text = LGNSString(dataDic[@"realname"]);
    NSString *age = ([dataDic[@"age"] integerValue]>0)?[NSString stringWithFormat:@"%@岁",dataDic[@"age"]]:@"";
    if (gender.length>0 && age.length>0) {
        self.ageLabel.text = [NSString stringWithFormat:@"%@ | %@",gender,age];
    }else if(gender.length>0){
        self.ageLabel.text = gender;
    }else if(age.length>0){
        self.ageLabel.text = age;
    }else{
        self.ageLabel.text = @"";
    }
    NSString *surgeryDate = LGNSString(dataDic[@"surgery_date"]);
    self.timeLabel.text = (surgeryDate.length>0)?[NSString stringWithFormat:@"最近手术：%@",surgeryDate]:@"";
    //标签
    for (UILabel *label in self.itemArray) {
        [label removeFromSuperview];
    }
    [self.itemArray removeAllObjects];
    NSArray *itemArray = dataDic[@"tips"];
    CGFloat marginTop = viewPix(38);
    CGFloat marginLeft = viewPix(100);
    for (NSString *content in itemArray) {
        CGFloat contentW = [content textWidthWithFont:LGFont(10) maxHeight:MAXFLOAT];
        UILabel *itemLabel = [UILabel lableWithText:content colorString:kThemeColor textFont:LGFont(10) textAlignment:NSTextAlignmentCenter lines:1];
        itemLabel.frame = CGRectMake(marginLeft, marginTop, contentW+viewPix(16), viewPix(16));
        itemLabel.backgroundColor = [UIColor colorWithString:@"#E6EEFF"];
        itemLabel.cornerRidus = viewPix(8);
        [self addSubview:itemLabel];
        [self.itemArray addObject:itemLabel];
        marginLeft = kMaxX(itemLabel.frame)+viewPix(8);
        //过多不显示
        if (marginLeft>Screen_W-viewPix(130)) {
            break;
        }
    }
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.backgroundColor = [UIColor clearColor];
    self.contentView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.iconBtn];
    [self.contentView addSubview:self.headPic];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.ageLabel];
    [self.contentView addSubview:self.timeLabel];
    [self.iconBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(viewPix(16));
        make.centerY.equalTo(self.headPic);
        make.width.height.equalTo(@(viewPix(20)));
    }];
    [self.headPic mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(viewPix(10));
        make.bottom.equalTo(self.contentView).offset(-viewPix(10));
        make.left.equalTo(self.iconBtn.mas_right).offset(viewPix(12));
        make.width.height.equalTo(@(viewPix(46)));
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headPic).offset(viewPix(2));
        make.left.equalTo(self.headPic.mas_right).offset(viewPix(8));
    }];
    [self.ageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.mas_right).offset(viewPix(6));
        make.centerY.equalTo(self.nameLabel);
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-viewPix(16));
        make.centerY.equalTo(self.nameLabel);
    }];
}

-(UIButton *)iconBtn{
    if(!_iconBtn){
        _iconBtn = [UIButton buttonWithImage:@"unselect20" selectImage:@"selected20"];
        _iconBtn.userInteractionEnabled = NO;
    }
    return _iconBtn;
}

-(UIImageView *)headPic{
    if(!_headPic){
        _headPic = [[UIImageView alloc]init];
        _headPic.backgroundColor = [UIColor colorWithString:@"#EAF4FF"];
        _headPic.cornerRidus = 8;
    }
    return _headPic;
}

-(UILabel *)nameLabel{
    if(!_nameLabel){
        _nameLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _nameLabel;
}

-(UILabel *)ageLabel{
    if(!_ageLabel){
        _ageLabel = [UILabel lableWithText:@"" colorString:@"#999999" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _ageLabel;
}

-(UILabel *)timeLabel{
    if(!_timeLabel){
        _timeLabel = [UILabel lableWithText:@"" colorString:@"#999999" textFont:LGFont(12) textAlignment:NSTextAlignmentRight lines:1];
    }
    return _timeLabel;
}

-(NSMutableArray *)itemArray{
    if(!_itemArray){
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}

@end
